package  ginger.code
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.geom.Matrix;
	/**
	 * 二维码画图
	 * @author Ning
	 */
	public class QrCodeView
	{
		/**
		 * 
		 * @param	frame           二进制数据
		 * @param	pixelPerPoint   缩放
		 * @param	outerFrame     
		 * @param	colorA         背景色
		 * @param	colorB
		 * @return
		 */
		public static function image(id:String,frame:Array, pixelPerPoint:Number = 4, outerFrame:int = 4, bgColor:uint = 0xffffff, codeColor:uint = 0x0e0e0e):Bitmap
		{
			var h:int = frame.length;
			var w:int = frame[0].length;
			
			var imgW:int = w + 2 * outerFrame;
			var imgH:int = h + 2 * outerFrame;
			
			var image:BitmapData = new BitmapData(imgW , imgH , false, bgColor);
			
			for(var y:int=0; y<h; y++) {
				for(var x:int=0; x<w; x++) {
					if (frame[y][x] == 1) {
						image.setPixel(x+outerFrame,y+outerFrame,codeColor);		 
					}
				}
			}
			
			//return image;
			var matrix:Matrix = new Matrix();
			matrix.scale(pixelPerPoint, pixelPerPoint);
			
			var bitData:BitmapData = new BitmapData(imgW*pixelPerPoint,imgH*pixelPerPoint,false,0xffffff);
			bitData.draw(image, matrix);
			image.dispose();
			image = null;
			var bmp:Bitmap = new Bitmap(bitData);
			bmp.name = id;
			return bmp;
		}
		
		/**
		 * 矢量图
		 * @param	frame
		 * @param	pixelPerPoint
		 * @param	outerFrame
		 * @param	bgColor
		 * @param	codeColor
		 */
		public static function vector(id:String,frame:Array, pixelPerPoint:Number = 4, outerFrame:int = 4, bgColor:uint = 0xffffff, codeColor:uint = 0x0e0e0e):Shape
		{
			var h:int = frame.length;
			var w:int = frame[0].length;
			
			var imgW:int = w + 2 * outerFrame;
			var imgH:int = h + 2 * outerFrame;
			
			var shape:Shape = new Shape();
			shape.name = id;
			shape.graphics.clear();
			shape.graphics.beginFill(bgColor);
			shape.graphics.drawRect(0, 0, imgW, imgH);
			shape.graphics.endFill();
			
			shape.graphics.beginFill(codeColor);
			
			for(var y:int=0; y<h; y++) {
				for(var x:int=0; x<w; x++) {
					if (frame[y][x] == 1) {
						shape.graphics.drawRect(x + outerFrame, y + outerFrame,1, 1);
					}
				}
			}
			shape.graphics.endFill();
			shape.scaleX = shape.scaleY = pixelPerPoint;
			return shape;
		}
	}

}